//
//  KZSearchVC.swift
//  BoyeStore
//
//  Created by 朱凯 on 2018/6/26.
//  Copyright © 2018年 itboye. All rights reserved.
//

import UIKit

class KZSearchVC: KZBaseViewControllerWithBack {
    
    var searchBar: KZSearchBar! = nil
    var hotSearchView = KZHotSearchingView()

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor.init(white: 0.96, alpha: 1)
        self.addNavigationItems(items: [""])
        setupUI()
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        setNavigationBackgroungAttributes(naviTranslucent: true,
                                          naviBgColor: .clear)
        
        self.navigationController?.navigationBar.isHidden = true
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.navigationController?.navigationBar.isHidden = false

    }
    deinit {
        print("销毁")
    }
}

// MARK: ------ 创建视图
extension KZSearchVC {
    
    func setupUI() {
        
        // 状态栏
        let statusBarBgView = UIView()
        statusBarBgView.backgroundColor = kThemeColor
        view.addSubview(statusBarBgView)
        statusBarBgView.snp.makeConstraints { (m) in
            m.left.top.right.equalTo(0)
            m.height.equalTo(UIApplication.shared.statusBarFrame.height)
        }
        
        // 搜索
        searchBar = KZSearchBar(searchClouse: { [weak self] (searchText) in
            let vc = KZSearchResultVC()
            vc.searchText = searchText
            self!.navigationController?.pushViewController(vc, animated: true)

        }, cancelClouse: { [weak self] in
            
            self!.navigationController?.popViewController(animated: true)
        })
        view.addSubview(searchBar)
        searchBar.snp.makeConstraints { (m) in
            m.left.right.equalTo(0)
            m.top.equalTo(statusBarBgView.snp.bottom).offset(10)
            m.height.equalTo(40)
        }
        
        // 热门搜索
        view.addSubview(hotSearchView)
        hotSearchView.list = [try! KZSearchItem.model(from: ["id": 1,
                                                        "content": "刮胡刀"]),
                              try! KZSearchItem.model(from: ["id": 2,
                                                        "content": "手表"]),
                              try! KZSearchItem.model(from: ["id": 3,
                                                        "content": "switch"]),
                              try! KZSearchItem.model(from: ["id": 4,
                                                        "content": "PS4"]),]
        hotSearchView.clickClouse = {
            [weak self] in
            let vc = KZSearchResultVC()
            vc.searchText = $0
            self!.navigationController?.pushViewController(vc, animated: true)
        }
        hotSearchView.snp.makeConstraints { (m) in
            m.left.right.equalTo(0)
            m.top.equalTo(searchBar.snp.bottom).offset(30)
        }
        
        
    }
}

